草庐IT

C++ 类 class

全部标签

c++ - 专门为私有(private)类(class)的功能?

有没有办法为私有(private)类专门化一个函数(比如,std::swap)?例如,当我测试这个时:#includeclassOuter{structInner{inta;voidswap(Inner&other){usingstd::swap;swap(this->a,other.a);}};public:staticvoidtest();};namespacestd{templatevoidswap(Outer::Inner&a,Outer::Inner&b){a.swap(b);}}voidOuter::test(){usingstd::swap;Innera,b;swap(a

c++ - 如何直接获取 rand() 序列中的第 n 个数字而不必调用 rand() n 次?

根据我的理解,使用特定种子设置srand会导致对rand()的调用序列每次为该特定种子生成相同的数字系列:例如:srand(seed1);rand()//firstnumber(e.g.:42)rand()//secondnumber(e.g:17)srand(seed1)rand()//firstnumber(sameasabove(42))rand()//secondnumber(sameasabove(17))有没有办法直接获取序列中的第n个数字,而不必调用rand()n次?例如,如果我想要系列中的第17个随机数,我想在一次调用中获得该数字,而不是调用rand()17次。我无法预

c++ - 嵌套类声明 : template vs non-template outer class

我有一个C++模板类,里面有一个嵌套类,比如:templateclassOuter_t{public:classInner;Inneri;};templateclassOuter_t::Inner{public:floatx;};intmain(){Outer_to_t;//3oranyarbitraryinto_t.i.x=1.0;return0;}编译没有任何问题。然而,一旦我声明了一个类似的非模板类,就像这样:classOuter_1{public:classInner;Inneri;};classOuter_1::Inner{public:floatx;};intmain(){

c++ - Gtest : Expected Class-Name Before '{'

我正在尝试将Gtest下的测试用例转换为使用测试夹具,以便在添加更多测试时可以使用通用设置。但是,这会导致错误:test_integrate.cc:4:47:error:expectedclass-namebefore'{'tokenclassIntegratorTest:public::testing::test{这种失败是我无法理解的,因为根据我的经验,它通常是由循环导入引起的,并且导入与工作代码相比没有变化。完整代码如下:#include"gtest/gtest.h"#include"utils/integrate.hpp"classIntegratorTest:public::

c++ - 为什么一个 const Class& 可以被初始化为自身?

今天我遇到了一个非常愚蠢但难以检测的错误。相关代码如下:classVector;classPointIterator{constVector&x;constVector&yv;PointIterator(constVector&xv,constVector&yvo):x(xv),yv(yv){;};//^^hereiswrong};为什么这样的代码是合法的C++?在任何情况下都可以使用yv变量吗?我知道关于intx=x+1;的类似问题,(请参阅thisquestion)但后者未正确初始化,您仍然可以使用x变量,而在上面的代码中,我认为您不能使用yv。奖励点:是否有任何编译选项可以让我检

c++ - C++ 中的 class a() 和 class a = class() 有什么区别?

来自Java和C#世界,一直喜欢用someclassa=someclass();代替someclassa();在C++中初始化一个类变量。但是,我的编译器有时会提示ErrorC2280:Attemptingtoreferenceadeletedfunction它们之间有什么区别吗?哪个更好? 最佳答案 Isthereanydifferencebetweenthem?一个大的:someclassa();isdeclaringafunction!someclassa=someclass();,在C++17'scopyellision之前

c++ - 关于 C 中的 C++ 类访问/操作

几周来我一直在阅读有关StackOverflow的问题...这将是我的第一个问题。最近我研究了如何让C访问/操作C++类。我知道在正常情况下,理想情况下不应分别用C和C++编译组件,但目前这不是一种选择。我查看了3个关于能够在C中移植/使用C++的教程。它们是:DevX上的“C++和C互操作性指南”“在同一程序中混合C和C++代码”一文Sun'ssite.[32]如何混合使用C和C++”,发表于Parashift首先,我已经知道的:你必须使用extern"C"来避免C++函数名称修饰。您需要与C语言兼容的回调原型(prototype)。G++必须将C++编译成.o文件,GCC将特定于C

c++ - 一个类(class)的规模是多少?

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:EmptyclassinC++classClass1{charc;};classClass2{};Class1和Class2的大小是多少?在VC6中,我同时获得了1.有人可以解释一下吗?

c++ - 额外的函数/方法定义会增加程序的内存占用吗?

在C++中,定义未使用的额外方法或函数是否会导致更大的内存占用或更慢的执行速度?基本上,我在一个类中有几个实用调试方法,没有一个是正常使用类所必需的。如果从未使用过这些定义,是否会保留这些定义,在内存占用或速度方面会有所不同吗?例如:classmyClass{public://SomethingtheuserofthisclasswoulduseintdoSomething(){...}//SomethingusedsolelytomakesureIwrotetheclassproperlyboolisClassValid(){...}};...myClassclassInstance

c++ - 在 C++ 中重新定义模板 <class T>

我已经搜索并搜索了我的问题的解决方案,但似乎找不到。我正在使用Code::Blocks,但出现了模板类的重定义错误。这是我的“vectorAux.h”文件:#ifndefvectoraux_h#definevectoraux_h#include#include#includetemplatevoidremoveDup(std::vector&v);templateunsignedseqVectSearch(conststd::vector&v,unsignedfirst,unsignedlast,constT&target);templatevoidwriteVector(consts